<?php
include "db.php";
$username = $_POST["username"] ?? "";
$password = $_POST["password"] ?? "";
$password1 = $_POST["password1"] ?? "";

if ($username =="" ||  $password =="" ){
    die("用户名和密码不能为空");
}
if ($password != $password1){
    die("两次密码不一致");
}

// 用户端唯一性
$sql = "select count(*) as total from user where username = '{$username}'";
$total = $db->read($sql)[0]["total"];
if($total>0){
    die("用户已存在");
}
// 密码加密
$password = password_hash($password,PASSWORD_DEFAULT);

// 插入数据
$sql = "insert into user (username,password) values ('{$username}','{$password}')";
$is = $db -> write($sql);
if ($is){
    header("refresh:1;url=login.php");
    echo "注册成功,1秒后自动跳转到登录页面";
}else{
    echo "写入失败，可能是SQL错误";
}
?>